//
//  CompleteUserInfoViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/5/27.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "CompleteUserInfoViewController.h"
#import "TitleContentAccessCell.h"
#import "SingleBtnCell.h"
#import "BeautyLightTabbarViewController.h"
#import "UserCenterViewModel.h"
#import "NSDate+Additions.h"
@interface CompleteUserInfoViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
}
@property(nonatomic,weak)IBOutlet UITableView *mainTableView;
@property(nonatomic,strong)NSArray *titleArray;
//值
@property(nonatomic,strong)NSString *birthday;
@property(nonatomic,strong)NSString *weight;
@property(nonatomic,strong)NSString *numberOfBaby;
@property(nonatomic,strong)NSString *monthsAfterBirth;
@property(nonatomic,strong)NSString *methodOfBirth;
@property(nonatomic,strong)NSString *monthHolidayCircle;
@property(nonatomic,strong)NSString *monthHolidayDays;
@property(nonatomic,strong)NSString *startTimeFromLastTime;
@property(nonatomic,strong)UserCenterViewModel *userCenterViewModel;
@end

@implementation CompleteUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (self.isFromUserCenter) {
        self.navigationItem.title = @"个人信息";
        [self setNavRightBtn:@"保存" color:nil];
    }else{
        self.navigationItem.title = @"完善信息";
        [self setNavRightBtn:@"跳过" color:nil];
    }
    if ([LoginTools sharedTools].userInfoModel) {
        NSString *userBirthDay = [NSString stringWithFormat:@"%@",[LoginTools sharedTools].userInfoModel.userBirthday];
       self.birthday = [NSDate timestampSwitchTime:[userBirthDay integerValue]/1000 andFormatter:@"yyyy-MM-dd"];
        self.weight = [NSString stringWithFormat:@"%@",[LoginTools sharedTools].userInfoModel.userWeight];
        self.numberOfBaby = [NSString stringWithFormat:@"%@",[LoginTools sharedTools].userInfoModel.userChildren];
        self.monthsAfterBirth = [NSString stringWithFormat:@"%@",[LoginTools sharedTools].userInfoModel.userChildbirth];
        self.methodOfBirth = [NSString stringWithFormat:@"%@",[LoginTools sharedTools].userInfoModel.userChildbirthType];
        self.monthHolidayCircle = [NSString stringWithFormat:@"%@",[LoginTools sharedTools].userInfoModel.userMenstrualCycle];
        self.monthHolidayDays = [NSString stringWithFormat:@"%@",[LoginTools sharedTools].userInfoModel.userMenstrualDays];
        self.startTimeFromLastTime = [NSString stringWithFormat:@"%@",[NSDate timestampSwitchTime:[[LoginTools sharedTools].userInfoModel.userMenstrualLastBeanDay integerValue]/1000 andFormatter:@"yyyy-MM-dd"]];
    }
    [self configTableView];
}
-(void)configTableView{
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableFooterView = [UIView new];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"TitleContentAccessCell" bundle:nil] forCellReuseIdentifier:@"TitleContentAccessCell"];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"SingleBtnCell" bundle:nil] forCellReuseIdentifier:@"SingleBtnCell"];
    self.mainTableView.showsVerticalScrollIndicator = NO;
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mainTableView.backgroundColor = [UIColor clearColor];
}
-(void)rightbuttonPressed:(id)sender{
    if (self.isFromUserCenter) {
        [SVProgressHUD show];
        [self.userCenterViewModel updateUserBirthday:self.birthday userWeight:self.weight userChildren:self.numberOfBaby userChildbirth:self.monthsAfterBirth userChildbirthType:self.methodOfBirth userMenstrualCycle:self.monthHolidayCircle userMenstrualDays:self.monthHolidayDays userMenstrualLastBeanDay:self.startTimeFromLastTime cmp:^{
            Toast(@"更新成功");
            [self.rt_navigationController popViewControllerAnimated:YES complete:nil];
        }];
    }else{
        BeautyLightTabbarViewController *tabVC = [[BeautyLightTabbarViewController alloc]init];
        RTRootNavigationController *nav = [[RTRootNavigationController alloc]initWithRootViewController:tabVC];
        [JumpTools setRootCtrl:nav];
    }
}
#pragma mark - UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.titleArray count]+!self.isFromUserCenter;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 2) {
        return 1;
    }else{
        return [[self.titleArray objectAtIndex:section] count];
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        return 64;
    }
    return 44;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        SingleBtnCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SingleBtnCell" forIndexPath:indexPath];
        return cell;
    }else{
        TitleContentAccessCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TitleContentAccessCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
        NSArray *titlesForRow = [self.titleArray objectAtIndex:indexPath.section];
        if (indexPath.row==0) {
            [cell.bgImageView setImage:[[UIImage imageNamed:@"topBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"topBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"topBg"].size.height/2]];
        }else if (indexPath.row<[titlesForRow count]-1){
            [cell.bgImageView setImage:[[UIImage imageNamed:@"middleBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"middleBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"middleBg"].size.height/2]];
        }else{
            [cell.bgImageView setImage:[[UIImage imageNamed:@"bottomBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"bottomBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"bottomBg"].size.height/2]];
        }
        cell.titleLab.text = [titlesForRow objectAtIndex:indexPath.row];
        switch (indexPath.section) {
            case 0:
            {
                switch (indexPath.row) {
                    case 0:
                    {
                        if (!EC_ISNullStr(self.birthday)) {
                            cell.contentText.text = [NSString stringWithFormat:@"%@",self.birthday];
                        }else{
                            cell.contentText.text = @"";
                        }
                    }
                        break;
                    case 1:
                    {
                        if (!EC_ISNullStr(self.weight)) {
                            cell.contentText.text = [NSString stringWithFormat:@"%@",self.weight];
                        }else{
                            cell.contentText.text = @"";
                        }
                    }
                        break;
                    case 2:
                    {
                        if (!EC_ISNullStr(self.numberOfBaby)) {
                            cell.contentText.text = [NSString stringWithFormat:@"%@",self.numberOfBaby];
                        }else{
                            cell.contentText.text = @"";
                        }
                    }
                        break;
                    case 3:
                    {
                        if (!EC_ISNullStr(self.monthsAfterBirth)) {
                            cell.contentText.text = [NSString stringWithFormat:@"%@",self.monthsAfterBirth];
                        }else{
                            cell.contentText.text = @"";
                        }
                    }
                        break;
                    case 4:
                    {
                        if (!EC_ISNullStr(self.methodOfBirth)) {
                            cell.contentText.text = [NSString stringWithFormat:@"%@",self.methodOfBirth];
                        }else{
                            cell.contentText.text = @"";
                        }
                    }
                        break;
                    default:
                        break;
                }
            }
                break;
            case 1:
            {
                switch (indexPath.row) {
                    case 0:
                    {
                        if (!EC_ISNullStr(self.monthHolidayCircle)) {
                            cell.contentText.text = [NSString stringWithFormat:@"%@",self.monthHolidayCircle];
                        }else{
                            cell.contentText.text = @"";
                        }
                    }
                        break;
                    case 1:
                    {
                        if (!EC_ISNullStr(self.monthHolidayDays)) {
                            cell.contentText.text = [NSString stringWithFormat:@"%@",self.monthHolidayDays];
                        }else{
                            cell.contentText.text = @"";
                        }
                    }
                        break;
                    case 2:
                    {
                        if (!EC_ISNullStr(self.startTimeFromLastTime)) {
                            cell.contentText.text = [NSString stringWithFormat:@"%@",self.startTimeFromLastTime];
                        }else{
                            cell.contentText.text = @"";
                        }
                    }
                        break;
                    default:
                        break;
                }
            }
                break;
            default:
                break;
        }
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                {
                    [SMBDatePickerView showDatePickerNormalPickerMode:SMBDatePickerTypeDate date:[NSDate date] completion:^(NSString *date) {
                        self.birthday = date;
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.mainTableView reloadData];
                        });
                    }];
                }
                    break;
                case 1:
                {
                    [SMBRadioPickerView showPickerViewWithStrings:@[@"45kg",@"46kg",@"47kg",@"48kg",@"49kg",@"50kg",@"51kg",@"52kg",@"53kg",@"54kg",@"55kg",@"56kg",@"57kg",@"58kg",@"59kg",@"60kg"] completion:^(NSString *selectedString) {
                        self.weight = selectedString;
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.mainTableView reloadData];
                        });
                    }];
                }
                    break;
                case 2:
                {
                    [SMBRadioPickerView showPickerViewWithStrings:@[@"1",@"2",@"3",@"4",@"5",@"6"] completion:^(NSString *selectedString) {
                        self.numberOfBaby = selectedString;
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.mainTableView reloadData];
                        });
                    }];
                }
                    break;
                case 3:
                {
                    [SMBRadioPickerView showPickerViewWithStrings:@[@"42D-3M",@"3M-12M",@"1Y-3Y",@"3Y以上"] completion:^(NSString *selectedString) {
                        self.monthsAfterBirth = selectedString;
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.mainTableView reloadData];
                        });
                    }];
                }
                    break;
                case 4:
                {
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"分娩方式" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
                    UIAlertAction *action = [UIAlertAction actionWithTitle:@"顺产" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        self.methodOfBirth = @"顺产";
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.mainTableView reloadData];
                        });
                    }];
                    [alert addAction:action];
                    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"剖腹产" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        self.methodOfBirth = @"剖腹产";
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.mainTableView reloadData];
                        });
                    }];
                    [alert addAction:action1];
                    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                        
                    }];
                    [alert addAction:action2];
                    dispatch_async(dispatch_get_main_queue(), ^{
                         [self presentViewController:alert animated:YES completion:nil];
                    });
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                {
                    NSMutableArray *list = [[NSMutableArray alloc]init];
                    for (int i=0; i<31; i++) {
                        [list addObject:[NSString stringWithFormat:@"%d天",i+1]];
                    }
                    [SMBRadioPickerView showPickerViewWithStrings:list completion:^(NSString *selectedString) {
                        self.monthHolidayCircle = selectedString;
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.mainTableView reloadData];
                        });
                    }];
                }
                    break;
                case 1:
                {
                    NSMutableArray *list = [[NSMutableArray alloc]init];
                    for (int i=0; i<31; i++) {
                        [list addObject:[NSString stringWithFormat:@"%d天",i+1]];
                    }
                    [SMBRadioPickerView showPickerViewWithStrings:list completion:^(NSString *selectedString) {
                        self.monthHolidayDays = selectedString;
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.mainTableView reloadData];
                        });
                    }];
                }
                    break;
                case 2:
                {
                    [SMBDatePickerView showDatePickerNormalPickerMode:SMBDatePickerTypeDate date:[NSDate date] completion:^(NSString *date) {
                        self.startTimeFromLastTime = date;
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.mainTableView reloadData];
                        });
                    }];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
}
-(NSArray*)titleArray{
    if (!_titleArray) {
        _titleArray = @[@[@"生日",@"体重",@"家有几宝",@"产后几月",@"分娩方式"],@[@"月经周期",@"经期天数",@"上次开始时间"]];
    }
    return _titleArray;
}
-(UserCenterViewModel*)userCenterViewModel{
    if (!_userCenterViewModel) {
        _userCenterViewModel = [[UserCenterViewModel alloc]init];
    }
    return _userCenterViewModel;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
